package com.esp.soccertutor.ui.adpater;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.esp.soccertutor.R;
import com.esp.soccertutor.ui.ShowCaseMoreInfo;
import com.esp.soccertutor.ui.bean.BeanProducts;
import com.esp.soccertutor.util.Const;
import com.esp.soccertutor.util.DownloadingFile;
import com.esp.soccertutor.util.Log;

public class ShowcaseProductAdapter extends ArrayAdapter<BeanProducts>{
	
	public String TAG = ShowcaseProductAdapter.class.getCanonicalName();
	
	static class ViewHolder {
		
		protected ImageView mImageView;
		protected TextView txtView_ProductName;
		protected TextView txtView_Desc;
		protected TextView txtView_priceUSD;
		protected TextView txtView_priceGBP;
		protected Button btn_ProductInfo;
		protected Button btn_Previews;
	}
	public ImageLoader imageLoader;
	private Context mContext;
	private ArrayList<BeanProducts> arrylstSCProducts;
	private int TypeID;
	
	public ShowcaseProductAdapter(Context context,ArrayList<BeanProducts> object,int TypeID) {
		super(context,R.layout.row_showcase_products,object);
		arrylstSCProducts = object;
		mContext = context;
		this.TypeID = TypeID;
		this.imageLoader = new ImageLoader(context);
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		View mView = null;
		if (convertView == null) {
				
				mView = LayoutInflater.from(mContext).inflate(R.layout.row_showcase_products,null);
				final ViewHolder viewHolder = new ViewHolder();
				viewHolder.mImageView = (ImageView)mView.findViewById(R.id.soccerImage);
				viewHolder.txtView_ProductName = (TextView)mView.findViewById(R.id.txtView_ProductName);
				viewHolder.txtView_Desc = (TextView)mView.findViewById(R.id.txtView_Desc);
				viewHolder.txtView_priceUSD = (TextView)mView.findViewById(R.id.txtView_priceUSD);
				viewHolder.txtView_priceGBP = (TextView)mView.findViewById(R.id.txtView_priceGBP);
				viewHolder.btn_ProductInfo = (Button)mView.findViewById(R.id.btn_ProductInfo);
				viewHolder.btn_Previews = (Button)mView.findViewById(R.id.btn_Previews);
				
				mView.setTag(viewHolder);
		}else {
			mView = convertView;
		}
		
		ViewHolder holder = (ViewHolder) mView.getTag();
		imageLoader.DisplayImage(arrylstSCProducts.get(position).getImageURL(),holder.mImageView);
		holder.txtView_ProductName.setText(arrylstSCProducts.get(position).getProductName());
		holder.txtView_Desc.setText(arrylstSCProducts.get(position).getProductDesc());
		holder.txtView_priceUSD.setText("Normal Price : "+ arrylstSCProducts.get(position).getPriceUSD());
		holder.txtView_priceGBP.setText("Introductory Price : " + arrylstSCProducts.get(position).getPriceGBP());
		holder.btn_ProductInfo.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//TODO GO TO Product Info 

				Intent mShowCaseIntent = new Intent(mContext,ShowCaseMoreInfo.class);
				mShowCaseIntent.putExtra(Const.SHOWCASE, Const.INT_SHOWCASE);
				mShowCaseIntent.putExtra(Const.CONTENT_ID,""+arrylstSCProducts.get(position).getProductID());
				mShowCaseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mContext.startActivity(mShowCaseIntent);
				
			}
		});
		holder.btn_Previews.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//TODO Start Downloading 
				Log.print(TAG,"Start Downloading ....");
				String extention = (TypeID == Const.TYPEID_EBOOKS ? ".epub" : ".3gp");
				Log.print(TAG,"URL :" + arrylstSCProducts.get(position).getPreviewFile());
				new DownloadingFile(mContext,arrylstSCProducts.get(position).getPreviewFile(),arrylstSCProducts.get(position).getProductName()+extention,TypeID,Const.INT_SHOWCASE).execute();
			}
		});
		
		
		return mView;
	}
	

}
